import time
import os

view_len = 10  # 容器显示的数量
bar_cells = 30 # 滚动条单位总数

# 测试内容
content = "\n".join([f"第{i}行内容" for i in range(1, 16)])

# 计算内容的行数
lines = content.split('\n')

# 总行数
total_lines = len(lines)

bar_len = int(round(view_len / total_lines * bar_cells))

def clear_screen():
    """清除屏幕"""
    os.system('cls' if os.name == 'nt' else 'clear')

def print_content(start):
    """打印指定范围内的内容"""
    for i in range(start, start + view_len):
        if i < len(lines):
            print(lines[i])

def calculate_scrollbar_position(position):
    """计算滚动条的位置"""
    
    scrollbar_position = int(round(position * bar_cells / total_lines))
    return scrollbar_position

def print_scrollbar(scrollbar_position):
    """打印滚动条"""
    scrollbar = '[' + '-' * scrollbar_position + '|' + '=' * bar_len + '|' + '-' * (bar_cells - scrollbar_position - bar_len) + ']'
    print('\n' + scrollbar_position, bar_len, bar_cells)
    print(scrollbar)

def print_scroll_status(position):
    """打印当前显示状态"""
    print(f"当前显示行数：{min(position + view_len, total_lines)} / {total_lines} \n")

def print_scroll(position):
    clear_screen()

    # 打印当前内容
    print_content(position)
    
    # 计算滚动条的位置
    scrollbar_position = calculate_scrollbar_position(position)
    
    # 打印滚动条
    print_scrollbar(scrollbar_position)
    
    # 打印显示状态
    print_scroll_status(position)

def print_scrollbar_content(content):
    """滚动条和内容的显示"""
    
    position = 0
    if position + view_len > total_lines:
        print_scroll(position)
    else:
        while True:
            print_scroll(position)
            if position + view_len >= total_lines:
                break
            # 等待并滚动
            time.sleep(0.5)
            
            # 更新位置
            position += 1

# 调用滚动条显示函数
print_scrollbar_content(content)
